package com.qixiaobao.web.controller.direct.invoice;

import com.qixiaobao.common.annotation.Log;
import com.qixiaobao.common.annotation.RepeatSubmit;
import com.qixiaobao.common.core.domain.AjaxResult;
import com.qixiaobao.common.core.page.TableDataInfo;
import com.qixiaobao.common.enums.BusinessType;
import com.qixiaobao.common.exception.UtilException;
import com.qixiaobao.common.utils.StringUtils;
import com.qixiaobao.common.utils.easyExcel.EasyExcelUtil;
import com.qixiaobao.direct.invoice.domain.dto.DirectInvoiceDTO;
import com.qixiaobao.direct.invoice.domain.vo.DirectInvoiceChargeVO;
import com.qixiaobao.direct.invoice.domain.vo.DirectWaitInvoiceVO;
import com.qixiaobao.direct.invoice.service.IDirectInvoiceNewService;
import com.qixiaobao.web.controller.core.controller.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Description 直投发票管理（新）
 * @Author yangxm
 * @Date 2025/7/3 15:15
 */
@Slf4j
@RestController
@RequestMapping("/direct/invoice/new")
public class DirectInvoiceNewController extends BaseController {


    @Autowired
    private IDirectInvoiceNewService directInvoiceNewService;

    /**
     * @Description 待开票 - 列表
     * @Author yangxm
     * @Date 2025/7/3 15:31
     * @param directInvoiceDTO 
     * @return com.qixiaobao.common.core.page.TableDataInfo
     */
    @RequestMapping("/wait/list")
    public TableDataInfo waitList(DirectInvoiceDTO directInvoiceDTO){
        directInvoiceDTO.setNoPermissionSupplyIdList(checkPermission());
        startPage();
        return getDataTable(directInvoiceNewService.selectWaitInvoiceList(directInvoiceDTO));
    }

    /**
     * @Description 待开票 - 开票金额合计
     * @Author yangxm
     * @Date 2025/7/3 15:31
     * @param directInvoiceDTO
     * @return com.qixiaobao.common.core.domain.AjaxResult
     */
    @GetMapping("/wait/total")
    public AjaxResult waitTotal(DirectInvoiceDTO directInvoiceDTO){
        directInvoiceDTO.setNoPermissionSupplyIdList(checkPermission());
        return success(directInvoiceNewService.selectWaitInvoiceTotal(directInvoiceDTO));
    }

    /**
     * @Description 待开票 - 导出
     * @Author yangxm
     * @Date 2025/7/3 15:31
     * @param directInvoiceDTO
     * @param response
     * @return void
     */
    @GetMapping("/wait/export")
    public void waitExport(DirectInvoiceDTO directInvoiceDTO, HttpServletResponse response){
        directInvoiceDTO.setNoPermissionSupplyIdList(checkPermission());
        List<DirectWaitInvoiceVO> list = directInvoiceNewService.selectWaitInvoiceList(directInvoiceDTO);
        try {
            EasyExcelUtil.writeExcel(response, list, DirectWaitInvoiceVO.class, "直投发票管理(新)-待开票", "sheet1");
        }catch (Exception e){
            log.error("直投发票管理(新)-待开票，导出失败：{}", e.getMessage());
            throw new UtilException("导出失败：" + e.getMessage());
        }
    }

    /**
     * @Description 开票中、已开票 - 列表
     * @Author yangxm
     * @Date 2025/7/3 15:31
     * @param directInvoiceDTO
     * @return com.qixiaobao.common.core.page.TableDataInfo
     */
    @GetMapping("/list")
    public TableDataInfo invoiceList(DirectInvoiceDTO directInvoiceDTO){
        startPage();
        return getDataTable(directInvoiceNewService.selectInvoiceList(directInvoiceDTO));
    }

    /**
     * @Description 开票中、已开票 - 列表 - 开票金额总计
     * @Author yangxm
     * @Date 2025/7/3 16:32
     * @param directInvoiceDTO
     * @return com.qixiaobao.common.core.domain.AjaxResult
     */
    @GetMapping("/total")
    public AjaxResult invoiceTotal(DirectInvoiceDTO directInvoiceDTO){
        return success(directInvoiceNewService.selectInvoiceTotal(directInvoiceDTO));
    }

    /**
     * @Description 开票中、已开票 - 导出
     * @Author yangxm
     * @Date 2025/7/3 16:36
     * @param directInvoiceDTO
     * @param response
     */
    @Log(title = "直投发票管理(新)-开票中、已开票", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void invoiceExport(DirectInvoiceDTO directInvoiceDTO, HttpServletResponse response){
        directInvoiceNewService.invoiceExport(directInvoiceDTO, response);
    }

    /**
     * @Description 开票中、已开票 - 费用详情
     * @Author yangxm
     * @Date 2025/7/3 16:47
     * @param directInvoiceDTO
     * @return com.qixiaobao.common.core.page.TableDataInfo
     */
    @GetMapping("/charge/list")
    public TableDataInfo invoiceChargeList(DirectInvoiceDTO directInvoiceDTO){
        startPage();
        return getDataTable(directInvoiceNewService.selectInvoiceChargeListByInvoiceId(directInvoiceDTO.getInvoiceId()));
    }

    /**
     * @Description 开票中、已开票 - 费用详情 - 导出
     * @Author yangxm
     * @Date 2025/7/3 17:06
     * @param directInvoiceDTO
     * @param response
     */
    @Log(title = "直投发票管理(新)-开票中、已开票", businessType = BusinessType.EXPORT, businessKey = "详情")
    @GetMapping("/charge/export")
    public void invoiceChargeExport(DirectInvoiceDTO directInvoiceDTO, HttpServletResponse response){
        if (directInvoiceDTO.getInvoiceStatus() == null){
            throw new UtilException("参数：开票状态不能为空！");
        }
        String insureStatusVal = "1".equals(directInvoiceDTO.getInvoiceStatus()) ? "开票中" : "2".equals(directInvoiceDTO.getInvoiceStatus()) ? "已开票" : "";
        if (StringUtils.isBlank(insureStatusVal)){
            throw new UtilException("参数：开票状态有误！");
        }
        String fileName = "直投发票管理(新)-" + insureStatusVal + "详情";
        List<DirectInvoiceChargeVO> list = directInvoiceNewService.selectInvoiceChargeListByInvoiceId(directInvoiceDTO.getInvoiceId());
        try {
            EasyExcelUtil.writeExcel(response, list, DirectInvoiceChargeVO.class, fileName, "sheet1");
        }catch (Exception e){
            throw new UtilException("导出失败：" + e.getMessage());
        }
    }

    /**
     * @Description 开票中 - 修改备注
     * @Author yangxm
     * @Date 2025/7/3 17:16
     * @param directInvoiceDTO
     * @return com.qixiaobao.common.core.domain.AjaxResult
     */
    @RepeatSubmit
    @Log(title = "直投发票管理(新)-开票中", businessType = BusinessType.UPDATE, businessKey = "供应商备注")
    @PutMapping("/update/supplyRemark")
    public AjaxResult updateSupplyRemark(@RequestBody DirectInvoiceDTO directInvoiceDTO){
        return toAjax(directInvoiceNewService.updateSupplyRemark(directInvoiceDTO));
    }

    /**
     * @Description 开票中 - 开票
     * @Author yangxm
     * @Date 2025/7/3 17:22
     * @param directInvoiceDTO
     * @return com.qixiaobao.common.core.domain.AjaxResult
     */
    @RepeatSubmit
    @Log(title = "直投发票管理(新)-开票中", businessType = BusinessType.IMPORT, businessKey = "发票文件")
    @PutMapping("/upload/invoiceFile")
    public AjaxResult uploadInvoiceFile(@RequestBody DirectInvoiceDTO directInvoiceDTO){
        return toAjax(directInvoiceNewService.uploadInvoiceFile(directInvoiceDTO));
    }

    /**
     * @Description 已开票 - 发票文件
     * @Author yangxm
     * @Date 2025/7/3 17:27
     * @param directInvoiceDTO
     * @return com.qixiaobao.common.core.domain.AjaxResult
     */
    @GetMapping("/invoiceFile")
    public AjaxResult invoiceFile(DirectInvoiceDTO directInvoiceDTO){
        return success(directInvoiceNewService.selectInvoiceFile(directInvoiceDTO));
    }
}
